//###############
//FILE : SocketAddress.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex2 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.communication;

/**
 * This class stores the ip and port of a socket and provides methods for comparing
 * and hashing them.
 * @author daniel_i_l
 *
 */
public class SocketAddress {
	private String _ip;
	private int _port;
	
	/**
	 * Create an address with the given ip and port.
	 * @param ip The ip of the address.
	 * @param port The port of the address.
	 */
	public SocketAddress(String ip, int port) {
		_ip = ip;
		_port = port;
	}
	
	/**
	 * Get the address ip.
	 * @return The ip address.
	 */
	public String getIp() {
		return _ip;
	}
	
	/**
	 * Get the address port.
	 * @return The port number.
	 */
	public int getPort() {
		return _port;
	}
	
	/**
	 * Check if two addresses are equal. Two addresses are equal iff they have the same
	 * ip address and port.
	 */
	public boolean equals (Object obj) {
		if (obj == null)
			return false;
		
		if (obj == this)
			return true;
		
		if (obj.getClass() != getClass())
			return false;
		
		SocketAddress rhs = (SocketAddress)obj;
		
		return (_ip.equals(rhs.getIp())) && (_port == rhs.getPort()); 
	}
	
	/**
	 * Get the hash code of the address.
	 */
	public int hashCode() {
		return _ip.hashCode() + _port;
	}
}
